package com.scu.phototab.util;

import com.scu.phototab.enums.ResponseEnum;
import com.scu.phototab.exception.ServerExpection;
import com.sun.istack.internal.Nullable;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by tianfei on 2017/4/7.
 */
public final class Predictions {

    public static <T> T checkNotNull(T reference){
        if(reference == null) {
            throw new ServerExpection(ResponseEnum.UNKONW_ERROR);
        } else {
            return reference;
        }
    }

    public static <T> T checkNotNull(T reference, @Nullable ResponseEnum error) {
        if(reference == null) {
            throw new ServerExpection(error);
        } else {
            return reference;
        }
    }

    public static void checkStringValueFormat(String value){

        if (value.contains("*")||
                value.contains("&")||
                value.contains("(")||
                value.contains(")")||
                value.contains("|")){

            throw  new ServerExpection(ResponseEnum.BAD_CHARACTER);
        }
    }

    public static boolean isEmailAddress(String value){

        Pattern pattern=Pattern.compile("^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$");
        Matcher matcher=pattern.matcher(value);

        return matcher.matches();
    }

}
